/**
 * 
 */
package com.wissen.giskard.client.controller;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.wissen.giskard.client.observer.LoginObserver;
import com.wissen.giskard.client.services.ServiceUtil;


/**
 * @author Swapnil Sonawane
 *
 * @created date :10-Aug-2010
 */
public class LoginController {

	private static LoginController _Instance=null;
	
	public static synchronized LoginController getInstance()
	{
		if(_Instance==null)
			_Instance=new LoginController();
		
		return _Instance;
	}
	
	List<LoginObserver> loginObserverList = new ArrayList<LoginObserver>();
	
	public void addLoginObserver(LoginObserver observer)
	{
		loginObserverList.add(observer);
	}
	
	public void validateUser(String username , String password)
	{
		
		ServiceUtil.getAuthenticateService().authenticateUser(username, password, loginCallback);
	}
	
	
	AsyncCallback<Boolean> loginCallback = new AsyncCallback<Boolean>() {

		@Override
		public void onFailure(Throwable caught) {
			for(LoginObserver observer:loginObserverList)
				observer.notifyLoginFail();
		}

		@Override
		public void onSuccess(Boolean result) {
			for(LoginObserver observer:loginObserverList)
				observer.notifyLoginSuccess(result);			
		}
	};
	
	
}
